package global.com.loansapplication.pay;

import android.app.Activity;

import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import global.com.loansapplication.base.BaseBiz;
import global.com.loansapplication.comm.MyApplication;
import global.com.loansapplication.ui.actviity.user.biz.RechargeBiz;
import global.com.loansapplication.util.LogUtils;

/**
 * Created by Administrator on 2017/11/2.
 */

public class WXPayManger implements IWXPayManger {

    public static final String APP_ID = "wxa371e93a55db1e64";
    private IWXAPI api;


    private Activity activity;

    public WXPayManger(Activity activity) {
        this.activity = activity;
    }

    RechargeBiz rechargeBiz = new RechargeBiz();

    private PayCallback payCallback;

    public void payWX(final Activity activity, String appid, String key, String noncestr, String packagesign, String partnerid, String prepayid, String sign, String timestamp) {
        api = WXAPIFactory.createWXAPI(activity, APP_ID, true);
        api.registerApp(APP_ID);
        PayReq req = new PayReq();
        req.appId = appid;
        req.partnerId = partnerid;
        req.prepayId = prepayid;
        req.packageValue = packagesign;
        req.nonceStr = noncestr;
        req.timeStamp = timestamp;
        req.sign = sign;
        req.extData = "app data";
        api.sendReq(req);
    }


    @Override
    public void recharge(String rechargeSettingId, final String payMethod, PayCallback payCallback) {
        this.payCallback = payCallback;
        rechargeBiz.wexiConfirm(MyApplication.userInfo.getUserCode(), rechargeSettingId, payMethod, new BaseBiz.Callback<WxBean>() {
            @Override
            public void onSuccess(WxBean wxBean) {
                        payWX(activity, wxBean.getAppid(), wxBean.getKey(), wxBean.getNoncestr(), wxBean.getmPackage(), wxBean.getPartnerid(), wxBean.getPrepayid(), wxBean.getSign(), wxBean.getTimestamp());

            }

            @Override
            public void onFailure(WxBean o) {
                LogUtils.d("2err:" + o);
            }
        });

    }
}
